//
// Created by Max on 2024/5/9.
//

#include <string.h>
#include "Properties.h"
#include "AnalysisFile.h"
#include "../MicLoader.h"

static const char *MIC_PROP_KEY = "vendor.haier.mic.num";
static const char *PRODUCT_KEY = "ro.product.device";

Properties *Properties::m_pProperties = nullptr;
std::mutex Properties::m_mutex;

Properties::Properties() {

}

Properties::~Properties() {

}

int Properties::property_get(const char *key, char *value, const char *default_value) {
    int len = __system_property_get(key, value);
    if (len > 0) {
        return len;
    }
    if (default_value) {
        len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
        memcpy(value, default_value, len);
        value[len] = '\0';
    }
    return len;
}

int Properties::property_set(const char *key, const char *value) {
    return __system_property_set(key, value);
}

Properties *Properties::instance() {
    m_mutex.lock();
    if (m_pProperties == nullptr) {
        m_pProperties = new Properties();
    }
    m_mutex.unlock();
    return m_pProperties;
}

int32_t Properties::getPropertyInt(const char *key, int32_t def) {
    int len;
    char *end;
    char buf[PROPERTY_VALUE_MAX] = {0};
    int32_t result = def;

    len = __system_property_get(key, buf);
    if (len > 0) {
        result = strtol(buf, &end, 0);
        if (end == buf) {
            result = def;
        }
    }

    return result;
}

int32_t Properties::getTvMicNum() {
    int len;
    int micNum = 4;
    char productName[PROPERTY_VALUE_MAX] = {0};
    len = __system_property_get(PRODUCT_KEY, productName);
    if (len > 0) {

        std::string strName = productName;
        LOGD("getTvMicNum  device name === %s", strName.c_str());
        if (strName.find("9655") != std::string::npos || strName.find("812") != std::string::npos
                                                         || strName.find("9653") != std::string::npos) {
            return 4; // 9655 812 都是 4 mic 平台
        }else if (strName.find("9633") != std::string::npos){
            return 2;
        }else if (strName.find("660") != std::string::npos || strName.find("670") != std::string::npos ||
                strName.find("570") != std::string::npos) {
            if (strName.find("_B") != std::string::npos) {
                micNum = 2;
            } else {
                micNum = 4;
            }
        }
    }
    return micNum;
}
